package com.sky.controller.admin;


import com.sky.dto.CateGoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@Api(tags = "分类管理接口模块")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;
    //分类条件查询
    @ApiOperation(value ="分类管理分类查询")
    @GetMapping("/page")
    public Result<PageResult> SelectPageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
    PageResult pageResult = categoryService.SelectPageQuery(categoryPageQueryDTO);
    return Result.success(pageResult);
    }
    //新增分类
    @ApiOperation(value ="新增分类")
    @PostMapping
    public  Result InsertCategory(@RequestBody CateGoryDTO cateGoryDTO){
        log.info("掺入的参数是{}", cateGoryDTO.toString());
        categoryService.InsertCategory(cateGoryDTO);
        return Result.success();
    }
    //更改状态信息
    @ApiOperation(value ="更改状态信息")
    @PostMapping("/status/{status}")
    public Result UpdateStatus(@PathVariable Integer status, Long id){
        Category category = Category.builder().id(id).status(status).build();
        log.info("更该状态信息{}", category.getStatus());
        categoryService.UpdateCategory(category);
        return Result.success();
    }
    //修改分类信息
    @ApiOperation(value ="修改分类信息")
    @PutMapping
    public Result UpdateCategory(@RequestBody CateGoryDTO categoryDTO){
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryService.UpdateCategory(category);
        return Result.success();
    }
    //根据id删除分类信息
    @ApiOperation(value ="根据id删除分类信息")
    @DeleteMapping
    public Result DeleteCategoryById(Long id){
        categoryService.DeleteCategoryById(id);
        return Result.success();
    }
    //根据类型查询分类
    @ApiOperation(value ="根据类型查询分类")
    @GetMapping("/list")
    public Result SelectCategoryByType(Integer type){
        CategoryPageQueryDTO categoryPageQueryDTO = new CategoryPageQueryDTO();
        categoryPageQueryDTO.setType(type);
        List<Category> categoryList =  categoryService.SelectCategoryByType(categoryPageQueryDTO);
        return Result.success(categoryList);
    }


}
